/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.widgets.messagebox;


private import dwt.dwt;

public import dwt.widgets.dialog;
private import dwt.widgets.display;
private import dwt.widgets.shell;
private import dwt.internal.win32.os;
private import dwt.util.util;


/**
 * Instances of this class are used used to inform or warn the user.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
 * <dd>OK, OK | CANCEL</dd>
 * <dd>YES | NO, YES | NO | CANCEL</dd>
 * <dd>RETRY | CANCEL</dd>
 * <dd>ABORT | RETRY | IGNORE</dd>
 * <dt><b>Events:</b></dt>
 * <dd>(none)</dd>
 * </dl>
 * <p>
 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
 * ICON_WARNING and ICON_WORKING may be specified.
 * </p><p>
 * IMPORTANT: This class is intended to be subclassed <em>only</em>
 * within the DWT implementation.
 * </p>
 */
public  class MessageBox : Dialog {

	// default title is the exe name
	static char[] defaultTitle;	
	char[] message;
	
	
public static char[] getDefaultTitle(){
	if(defaultTitle is null)
	{
		defaultTitle = Util.getExePathName(0, false);
		int i ;
		for( i = defaultTitle.length -1 ; i >= 0; i -- ){
			if( defaultTitle[i] is '/' || defaultTitle[i] is '\\' ){
				defaultTitle	= defaultTitle[ i + 1 .. $ ];
				break;
			}
		}
		for( i = defaultTitle.length -1 ; i >= 0; i -- ){
			if( defaultTitle[i] is '.') break;
		}
		if( i > 0 ){
			defaultTitle = defaultTitle[0 .. i ]; 
		}
	}
	return defaultTitle; 
}
/**
 * Constructs a new instance of this class given only its parent.
 *
 * @param parent a shell which will be the parent of the new instance
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 */
public this (Shell parent) {
	this (parent, DWT.OK | DWT.ICON_INFORMATION | DWT.APPLICATION_MODAL);
}

/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>DWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>DWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 *
 * @param parent a shell which will be the parent of the new instance
 * @param style the style of dialog to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 */
public this (Shell parent, int style) {
	super (parent, checkStyle (style));
	checkSubclass ();
}

// <Shawn>
// actually, the parent can be null
void checkParent (Shell parent){
}

static int checkStyle (int style) {
	if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) == 0) style |= DWT.APPLICATION_MODAL;
	int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE);
	int bits = style & mask;
	if (bits == DWT.OK || bits == DWT.CANCEL || bits == (DWT.OK | DWT.CANCEL)) return style;
	if (bits == DWT.YES || bits == DWT.NO || bits == (DWT.YES | DWT.NO) || bits == (DWT.YES | DWT.NO | DWT.CANCEL)) return style;
	if (bits == (DWT.RETRY | DWT.CANCEL) || bits == (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style;
	style = (style & ~mask) | DWT.OK;
	return style;
}

/**
 * Returns the dialog's message, which is a description of
 * the purpose for which it was opened. This message will be
 * visible on the dialog while it is open.
 *
 * @return the message
 */
public char[] getMessage () {
	return (message);
}

/**
 * Makes the dialog visible and brings it to the front
 * of the display.
 *
 * @return the ID of the button that was selected to dismiss the
 *         message box (e.g. DWT.OK, DWT.CANCEL, etc...)
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
 * </ul>
 */
public int open () {

	/* Compute the MessageBox style */
	int buttonBits = 0;
	if ((style & DWT.OK) == DWT.OK) buttonBits = OS.MB_OK;
	if ((style & (DWT.OK | DWT.CANCEL)) == (DWT.OK | DWT.CANCEL)) buttonBits = OS.MB_OKCANCEL;
	if ((style & (DWT.YES | DWT.NO)) == (DWT.YES | DWT.NO)) buttonBits = OS.MB_YESNO;
	if ((style & (DWT.YES | DWT.NO | DWT.CANCEL)) == (DWT.YES | DWT.NO | DWT.CANCEL)) buttonBits = OS.MB_YESNOCANCEL;
	if ((style & (DWT.RETRY | DWT.CANCEL)) == (DWT.RETRY | DWT.CANCEL)) buttonBits = OS.MB_RETRYCANCEL;
	if ((style & (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) == (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) buttonBits = OS.MB_ABORTRETRYIGNORE;
	if (buttonBits == 0) buttonBits = OS.MB_OK;

	int iconBits = 0;
	if ((style & DWT.ICON_ERROR) != 0) iconBits = OS.MB_ICONERROR;
	if ((style & DWT.ICON_INFORMATION) != 0) iconBits = OS.MB_ICONINFORMATION;
	if ((style & DWT.ICON_QUESTION) != 0) iconBits = OS.MB_ICONQUESTION;
	if ((style & DWT.ICON_WARNING) != 0) iconBits = OS.MB_ICONWARNING;
	if ((style & DWT.ICON_WORKING) != 0) iconBits = OS.MB_ICONINFORMATION;

	/* Only MB_APPLMODAL is supported on WinCE */
	int modalBits = 0;
	version(WinCE) {
		if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) != 0) {
			modalBits = OS.MB_APPLMODAL;
		}
	} else {
		if ((style & DWT.PRIMARY_MODAL) != 0) modalBits = OS.MB_APPLMODAL;
		if ((style & DWT.APPLICATION_MODAL) != 0) modalBits = OS.MB_TASKMODAL;
		if ((style & DWT.SYSTEM_MODAL) != 0) modalBits = OS.MB_SYSTEMMODAL;
	}

	int bits = buttonBits | iconBits | modalBits;
	if ((style & DWT.RIGHT_TO_LEFT) != 0) bits |= OS.MB_RTLREADING;
	if ((style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT)) == 0) {
		if (parent !is null && (parent.style & DWT.MIRRORED) != 0) {
			bits |= OS.MB_RTLREADING;
		}
	}
	
	/*
	* Feature in Windows.  System modal is not supported
	* on Windows 95 and NT.  The fix is to convert system
	* modal to task modal.
	*/
	if ((bits & OS.MB_SYSTEMMODAL) != 0) {
		bits |= OS.MB_TASKMODAL;
		bits &= ~OS.MB_SYSTEMMODAL;
	}

	/*
	* Feature in Windows.  In order for MB_TASKMODAL to work,
	* the parent int of the MessageBox () call must be NULL.
	* If the parent is not NULL, MB_TASKMODAL behaves the
	* same as MB_APPLMODAL.  The fix to set the parent int
	* anyway and not rely on MB_MODAL to work by making the
	* parent be temporarily modal. 
	*/
	HWND hwndOwner = parent !is null ? parent.handle : null;
	Shell oldModal = null;
	Display display = null;
	if ((bits & OS.MB_TASKMODAL) != 0 ) {
		// <Shawn> modified here, parent may be null
		display = parent ? parent.getDisplay () : Display.getCurrent();
		oldModal = display.getModalDialogShell ();
		display.setModalDialogShell (parent);
	}

	/* Open the message box */
	/* Use the character encoding for the default locale */
	int code = OS.MessageBox (hwndOwner, Converter.StrToTCHARz(message), Converter.StrToTCHARz(title), bits);
	
	/* Clear the temporarily dialog modal parent */
	if ((bits & OS.MB_TASKMODAL) != 0) {
		display.setModalDialogShell (oldModal);
	}
	
	/*
	* This code is intentionally commented.  On some
	* platforms, the owner window is repainted right
	* away when a dialog window exits.  This behavior
	* is currently unspecified.
	*/
//	if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
	
	/* Compute and return the result */
	if (code != 0) {
		int type = bits & 0x0F;
		if (type == OS.MB_OK) return DWT.OK;
		if (type == OS.MB_OKCANCEL) {
			return (code == OS.IDOK) ? DWT.OK : DWT.CANCEL;
		}
		if (type == OS.MB_YESNO) {
			return (code == OS.IDYES) ? DWT.YES : DWT.NO;
		}
		if (type == OS.MB_YESNOCANCEL) {
			if (code == OS.IDYES) return DWT.YES;
			if (code == OS.IDNO) return DWT.NO;
			return DWT.CANCEL;
		}
		if (type == OS.MB_RETRYCANCEL) {
			return (code == OS.IDRETRY) ? DWT.RETRY : DWT.CANCEL;
		}
		if (type == OS.MB_ABORTRETRYIGNORE) {
			if (code == OS.IDRETRY) return DWT.RETRY;
			if (code == OS.IDABORT) return DWT.ABORT;
			return DWT.IGNORE;
		}
	}
	return DWT.CANCEL;
}

/**
 * Sets the dialog's message, which is a description of
 * the purpose for which it was opened. This message will be
 * visible on the dialog while it is open.
 *
 * @param string the message
 * 
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the string is null</li>
 * </ul>
 */
public void setMessage (char[] string) {
	if (string !is null) 
		message = string;
}


public static int showMessage(char[] str, char[] title = null, Shell shell = null, int style = DWT.OK | DWT.ICON_WARNING)
{
	MessageBox msgBox = new MessageBox(shell, style);
	msgBox.setMessage(str);
	if(title is null){
		title = getDefaultTitle();
	}
	msgBox.setText(title);
	return msgBox.open();
}

alias showMessage showMsg;

}


